package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * This entity represents type of roles which employee can have in companies he/she works in.
 * Examples: CEO, Manager, Team Leader, etc.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="ROLE_TYPE")
@NamedQueries({
	@NamedQuery(name="findRoleTypeByCode", query="SELECT rt FROM RoleTypeEntityImpl rt WHERE rt.code = :code")
})
public class RoleTypeEntityImpl extends CommonPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = -1890176039634399662L;

	/*
	 * Entity attributes (some inherited).
	 */

	/*
	 * Relations
	 */
	/**
	 * Relation to RoleEntityImpl instances created with this RoleTypeEntityImpl
	 */
	@OneToMany(fetch=FetchType.EAGER, targetEntity=RoleEntityImpl.class, mappedBy="roleType") // no cascade!
	//@OnDelete(action=OnDeleteAction.NO_ACTION)
	private Set<RoleEntityImpl> roles;

	/**
	 * Required medical examination which person must pass in order to fulfill this RoleTypeEntityImpl's requirements
	 */
	@ManyToMany(fetch=FetchType.EAGER, targetEntity=MedicalExaminationTypeForRoleTypeEntityImpl.class, cascade={CascadeType.REMOVE})
	@JoinTable(name="MAP_RT__MET_2")
	private Set<MedicalExaminationTypeForRoleTypeEntityImpl> medExamTypesForRoleTypes;

	/**
	 * Required trainings which person must pass in order to fulfill this RoleTypeEntityImpl's requirements
	 */
	@ManyToMany(fetch=FetchType.EAGER, targetEntity=TrainingTypeForRoleTypeEntityImpl.class, cascade={CascadeType.REMOVE})
	@JoinTable(name="MAP_RT__TT_FRT")
	private Set<TrainingTypeForRoleTypeEntityImpl> traningTypesForRoleTypes;


	public RoleTypeEntityImpl()
	{
		super(null, null, null);
	}

	public RoleTypeEntityImpl(String roleCode, String roleName, String roleDescription)
	{
		super(roleCode, roleName, roleDescription);
	}

	public Set<RoleEntityImpl> getRoles()
	{
		return roles;
	}

	public List<RoleEntityImpl> getRolesAsList()
	{
		return new ArrayList<RoleEntityImpl>(getRoles());
	}

	public Set<MedicalExaminationTypeForRoleTypeEntityImpl> getMedicalExaminationTypeForRoleType()
	{
		return medExamTypesForRoleTypes;
	}

	public List<MedicalExaminationTypeForRoleTypeEntityImpl> getMedicalExaminationTypeForRoleTypeAsList()
	{
		return new ArrayList<MedicalExaminationTypeForRoleTypeEntityImpl>(getMedicalExaminationTypeForRoleType());
	}

	public void addMedicalExaminationTypeForRoleType(MedicalExaminationTypeForRoleTypeEntityImpl medicalExaminationTypeForRoleType)
	{
		if (this.medExamTypesForRoleTypes == null)
			this.medExamTypesForRoleTypes = new HashSet<MedicalExaminationTypeForRoleTypeEntityImpl>();
		this.medExamTypesForRoleTypes.add(medicalExaminationTypeForRoleType);
	}

	public Set<TrainingTypeForRoleTypeEntityImpl> getTraningTypesForRoleTypes()
	{
		return traningTypesForRoleTypes;
	}

	public List<TrainingTypeForRoleTypeEntityImpl> getTraningTypesForRoleTypesAsList()
	{
		return new ArrayList<TrainingTypeForRoleTypeEntityImpl>(getTraningTypesForRoleTypes());
	}

	public void addTrainingTypeForRoleType(TrainingTypeForRoleTypeEntityImpl traningTypesForRoleType)
	{
		if (this.traningTypesForRoleTypes == null)
			this.traningTypesForRoleTypes = new HashSet<TrainingTypeForRoleTypeEntityImpl>();
		this.traningTypesForRoleTypes.add(traningTypesForRoleType);
	}

    
  public Set<MedicalExaminationTypeForRoleTypeEntityImpl> getMedExamTypesForRoleTypes()
  {
    return medExamTypesForRoleTypes;
  }

  
  public void setMedExamTypesForRoleTypes(Set<MedicalExaminationTypeForRoleTypeEntityImpl> medExamTypesForRoleTypes)
  {
    this.medExamTypesForRoleTypes = medExamTypesForRoleTypes;
  }

  
  public void setRoles(Set<RoleEntityImpl> roles)
  {
    this.roles = roles;
  }

  
  public void setTraningTypesForRoleTypes(Set<TrainingTypeForRoleTypeEntityImpl> traningTypesForRoleTypes)
  {
    this.traningTypesForRoleTypes = traningTypesForRoleTypes;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("RoleTypeEntityImpl [toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.RoleType#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getRoles().size() == 0;
	}
}
